//
//  ZZNumDebtRankSelectedView.m
//  新项目
//
//  Created by liGordon on 17/3/16.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZNumDebtRankSelectedView.h"
@interface ZZNumDebtRankSelectedView ()<UITextFieldDelegate>
@property (nonatomic,strong) UIDatePicker *datePicker;

@property(nonatomic,strong) UIToolbar *toolBar;
@property (nonatomic, assign) NSInteger tagNum;
@property(nonatomic,strong) UIToolbar *toolBar1;
@end
@implementation ZZNumDebtRankSelectedView

+(instancetype)ViewWithXib
{
    ZZNumDebtRankSelectedView *view=[[[NSBundle mainBundle] loadNibNamed:@"ZZNumDebtRankSelectedView" owner:nil options:nil]lastObject];
    return view;
    
}
- (IBAction)clickForSeletecd:(id)sender {
    if ([self.delegate respondsToSelector:@selector(clickForSeletectedDataWithRepairTime:andOwnerName:)]) {
        if (self.carOwnerField.text.length==0||self.carOwnerField.text==nil||[[self.carOwnerField.text class] isSubclassOfClass:[NSNull class]]) {
            self.carOwnerField.text=@"";
        }
        if (self.TimeField.text.length==0||self.TimeField.text==nil||[[self.TimeField.text class] isSubclassOfClass:[NSNull class]]) {
            self.TimeField.text=@"";
        }
        [self.delegate clickForSeletectedDataWithRepairTime:self.TimeField.text andOwnerName:self.carOwnerField.text];
        
    }
}
- (UIToolbar *)toolBar {
    if (_toolBar == nil) {
        // 1.创建键盘工具条
        UIToolbar *toolbar = [[UIToolbar alloc] init];
        toolbar.frame = CGRectMake(0, 0, 320, 44);
        toolbar.barTintColor = blueColor;
        toolbar.tintColor = [UIColor whiteColor];
        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        // 4. 完成
        UIBarButtonItem *complete = [[UIBarButtonItem alloc] initWithTitle:@"确定" style: UIBarButtonItemStyleDone target:self action:@selector( complete)];
        UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"选择时间" style: UIBarButtonItemStyleDone target:self action:@selector(chooseEndTime)];
        toolbar.items = @[flexibleSpace,next,complete];
        _toolBar = toolbar;
    }
    return _toolBar;
}
//隐藏
- (IBAction)clickForHiden:(id)sender {
    if ([self.delegate respondsToSelector:@selector(hiddenTheView)]) {
        [self.delegate hiddenTheView];
    }
}

-(void)awakeFromNib
{
    [super awakeFromNib];
}

#pragma mark - 监听按钮的点击事件
- (void)complete{
    
    [self endEditing:YES];
    
    
}

-(void)chooseEndTime
{
    [self.TimeField becomeFirstResponder];
}
#pragma mark - 懒加载控件
- (UIDatePicker *)datePicker {
    
    DLog(@"创建了日期选择器");
    // 创建日期选择器
    // 一般不需要设置宽高
    _datePicker = [[UIDatePicker alloc] init];
    // 设置时区
    _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    // 设置时间模式
    _datePicker.datePickerMode = UIDatePickerModeDate;
    // 设置最小选择时间
    //        _datePicker.minimumDate =[NSDate date];
    //[NSDate dateWithTimeIntervalSinceNow:-(365 * 20 * 24 * 3600)];
    //获取当前的值
    NSDateFormatter *matter=[[NSDateFormatter alloc] init];
    NSString *time=self.TimeField.text;
    matter.dateFormat=@"yyyy年MM月dd";
    NSDate *date=[matter dateFromString:time];
    // 设置最大选择时间
    _datePicker.maximumDate = date;
    // 设置时间间隔
    _datePicker.minuteInterval = 2;
    
    // 监听时间选择器值改变时间
    // 提示：除了按钮，其它能够添加事件的控件，一般都是UIControlEventValueChanged事件
    [_datePicker addTarget:self action:@selector(dateValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    return _datePicker;
}


/**
 *  监听日期改变事件
 */
- (void)dateValueChanged:(UIDatePicker *)datePicker {
    // 获得当前显示的时间
    NSDate *date = datePicker == nil? [NSDate date]: datePicker.date;
    // 创建日期格式化对象
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 设置时间格式
    fmt.dateFormat  =@"yyyy-MM-dd";
    // 将日期转换成字符串
    NSString *timeStr =[fmt stringFromDate:date];

    self.TimeField.text = timeStr;
}

@end
